window.onload = function () {
    document.getElementById("user_list").className = "add_class";
    document.getElementById("attendance").className = "add-nav-active";
};

function click_ul() {
    if (document.getElementById("jiantou").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou").className = "iconfont icon-jiantou-xia";
        document.getElementById("organizational_management_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou").className = "iconfont icon-jiantou-shang";
        document.getElementById("organizational_management_ul").style.display = "none";
    }
}

function click_ul1() {
    if (document.getElementById("jiantou1").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou1").className = "iconfont icon-jiantou-xia";
        document.getElementById("gebruikersrol_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou1").className = "iconfont icon-jiantou-shang";
        document.getElementById("gebruikersrol_ul").style.display = "none";
    }
}

function click_ul2() {
    if (document.getElementById("jiantou2").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou2").className = "iconfont icon-jiantou-xia";
        document.getElementById("SuperSU_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou2").className = "iconfont icon-jiantou-shang";
        document.getElementById("SuperSU_ul").style.display = "none";
    }
}

function create_user() {
    var local_host = window.location.host;
    document.getElementById("loading").style.display='inline-block';
    var form = new FormData();
    var real_name = document.getElementById("real_name").value;
    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;
    var department = document.getElementById("department").value;
    var gender = document.getElementById("gender").value;
    var education = document.getElementById("education").value;
    var marital_status = document.getElementById("marital_status").value;
    var political_outlook = document.getElementById("political_outlook").value;
    var nation = document.getElementById("nation").value;
    var ID_number = document.getElementById("ID_number").value;
    var native_place = document.getElementById("native_place").value;
    var major = document.getElementById("major").value;
    var graduated_university = document.getElementById("graduated_university").value;
    var position = document.getElementById("position").value;
    var home_address = document.getElementById("home_address").value;
    var mobile = document.getElementById("mobile").value;
    var mailbox = document.getElementById("mailbox").value;
    var remarks = document.getElementById("remarks").value;
    var signature_picture = document.getElementById("signature_picture_url_img").src.replace("/media/", "");
    var enclosure = document.getElementById("enclosure").files[0];

    var xhr=new XMLHttpRequest();
    xhr.open('post', "/personnel/new_user.html", true);
    form.append('real_name', String(real_name));
    form.append('username', String(username));
    form.append('password', String(password));
    form.append('department', String(department));
    form.append('gender', String(gender));
    form.append('education', String(education));
    form.append('marital_status', String(marital_status));
    form.append('political_outlook', String(political_outlook));
    form.append('nation', String(nation));
    form.append('ID_number', String(ID_number));
    form.append('native_place', String(native_place));
    form.append('major', String(major));
    form.append('graduated_university', String(graduated_university));
    form.append('position', String(position));
    form.append('home_address', String(home_address));
    form.append('mobile', String(mobile));
    form.append('mailbox', String(mailbox));
    form.append('remarks', String(remarks));
    form.append('signature_picture', signature_picture);
    form.append('enclosure', enclosure);

    xhr.send(form);
    xhr.onreadystatechange=function(){
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display='none';
                var JsonDate = JSON.parse(xhr.responseText);
                if (JsonDate["code"] === 200){
                    alert("用户添加成功！");
                }
                else {
                    alert(JsonDate["message"])
                }
            }
        }
    }
}

function upload_signature_picture() {
    // document.getElementById("loading").style.display = "inline";
    var form = new FormData();
    var files = document.getElementById("signature_picture").files[0];
    console.log(files)
    var username = document.getElementById("username").value;

    form.append('files', files);
    form.append('username', username);
    form.append('add_user', "True");

    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;
    xhr.open('post', "/system-setup/update-signature-picture", true);
    xhr.send(form);
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);

                if (JsonDate['code']===200){
                    document.getElementById("signature_picture_url_img").style.display='inline-block';
                    document.getElementById("signature_picture_url_img").src = JsonDate['data']["signature_picture"];
                    document.getElementById("signature_picture").value="";
                }
                else{
                    alert(JsonDate['message'])
                }

            }
        }
    }
}